package org.example.utils.post.post;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.example.partten.builder.product.post.PPostDto;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.es.doc.EsDocUtil;
import org.example.skytool.es.doc.EsDslUtil;
import org.example.utils.post.post.dto.EsPostCompletionDoc;
import org.example.utils.post.post.dto.EsPostSearchDoc;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static org.example.skytool.es.EsConstarnt.*;

/**
 * @author 22449
 */
@Service
public class EsPostService {

    @Resource
    private EsDocUtil esDocUtil;

    @Resource
    private EsDslUtil esDslUtil;

    @Resource
    private IdWorker idWorker;

    private String name = POST_INDEX;

    private String content = POST_CONTENT_DOC;
    private String hot = POST_HOT_DOC;
    private String themeId = POST_THEME_DOC;

    private String content_name = POST_SEARCH_INDEX;

    private String content_content = POST_SEARCH_CONTENT_DOC;


    public void insertDocument(PPostDto postDto) {
        EsPostSearchDoc esPostSearchDoc = new EsPostSearchDoc(postDto.getPPost());
        esDocUtil.insertDocument(postDto.getPPost().getId().toString(), name, esPostSearchDoc);
    }

    public void insertSearchContent(String content){
        EsPostCompletionDoc esPostSearchDoc = new EsPostCompletionDoc(content);
        esDocUtil.insertDocument(idWorker.nextId().toString(),content_name,esPostSearchDoc);
    }

    public EsDslUtil.DslResult<EsPostCompletionDoc> querySuggest(String content){
        SearchRequest searchRequest = esDslUtil.getSearchRequest(content_name);
        searchRequest.source().suggest(new SuggestBuilder().addSuggestion(
                "campus",
                SuggestBuilders.completionSuggestion(this.content_content)
                        .prefix(content)
                        .skipDuplicates(true)
                        .size(10)
        ));
        return esDslUtil.sendQuery(searchRequest,EsPostCompletionDoc.class);
    }

    public EsDslUtil.DslResult<EsPostSearchDoc> searchDoc(Integer isValid, Long themeId, String content, Integer start, Integer limit) {
        SearchRequest searchRequest = esDslUtil.getSearchRequest(name);
        searchRequest.source().from(start).size(limit);

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery(this.content, content));
        if (themeId != 0) {
            boolQueryBuilder.filter(QueryBuilders.termQuery(this.themeId, themeId));
        }

        if (isValid == 0) {

        } else if (isValid == 1) {

        } else if (isValid == 2) {

        }

        return esDslUtil.sendQuery(searchRequest,EsPostSearchDoc.class);
    }
}

